package com.qiuzhiyi.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.qiuzhiyi.Const;
import com.qiuzhiyi.R;
import com.qiuzhiyi.activity.JobDetailActivity;
import com.qiuzhiyi.activity.SearchActivity;
import com.qiuzhiyi.activity.ZhaoGongActivity;
import com.qiuzhiyi.adapter.ZhaoGongAdapter;
import com.qiuzhiyi.base.QZYBaseFragment;
import com.qiuzhiyi.item.BannerItem;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.ZhaoGongItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/13 0013
 * Description: "找工作"
 */

public class FindJobFragment extends QZYBaseFragment implements View.OnClickListener {
    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.layout_search)
    LinearLayout mLayoutSearch;
    @BindView(R.id.txt_search)
    TextView mTxtSearch;
    private RollPagerView mRollPagerHeader;

    private ZhaoGongAdapter mAdapter;
    private List<ZhaoGongItem.ListBean> mZhaoGongItemList;
    private String mPre="http://obxgyv3if.bkt.clouddn.com/appimages/";

    public static FindJobFragment newInstance() {

        Bundle bundle = new Bundle();

        FindJobFragment fragment = new FindJobFragment();
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find_job;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTxtSearch.setOnClickListener(this);
        setSmartRefreshLayout(mRefreshLayout);
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.layout_home_header, null);
        mRollPagerHeader = headerView.findViewById(R.id.pager_header);
        headerView.findViewById(R.id.layout_zhaogong).setOnClickListener(this);
        headerView.findViewById(R.id.layout_linshigong).setOnClickListener(this);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new ZhaoGongAdapter(R.layout.item_zhaogong, "1");
        mAdapter.bindToRecyclerView(mRecycler);

        mAdapter.setHeaderView(headerView);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", String.valueOf(mZhaoGongItemList.get(position).getCId()));
                bundle.putString("type", "1");
                bundle.putString("salary", mZhaoGongItemList.get(position).getCCapital());
                bundle.putString("name", mZhaoGongItemList.get(position).getCName());
                startWithArg(JobDetailActivity.class, bundle);
            }
        });
        mRollPagerHeader.setPlayDelay(3000);
        getBanner();


        showList("1");
        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int alpha = recyclerView.computeVerticalScrollOffset();
                alpha = alpha < 0 ? 0 : alpha;
                alpha = alpha > 255 ? 255 : alpha;
                mLayoutSearch.setBackgroundColor(Color.argb(alpha, 56, 200, 255));
            }
        });
        mRefreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {

            @Override
            public void onHeaderPulling(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
                if (percent > 0) {
                    if (mLayoutSearch.getVisibility() == View.VISIBLE) {
                        mLayoutSearch.setVisibility(View.GONE);
                    }
                } else {
                    if (mLayoutSearch.getVisibility() == View.GONE) {
                        mLayoutSearch.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onHeaderReleasing(RefreshHeader header, float percent, int offset, int headerHeight, int extendHeight) {
                if (percent == 0) {
                    if (mLayoutSearch.getVisibility() == View.GONE) {
                        mLayoutSearch.setVisibility(View.VISIBLE);
                    }
                }
            }

        });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        showList("1");
        getBanner();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        showList(String.valueOf(mPage + 1));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_zhaogong:
                Bundle bundle = new Bundle();
                bundle.putString("type", "1");
                startWithArg(ZhaoGongActivity.class, bundle);
                break;
            case R.id.layout_linshigong:
                Bundle bundle1 = new Bundle();
                bundle1.putString("type", "2");
                startWithArg(ZhaoGongActivity.class, bundle1);
                break;
            case R.id.txt_search:
                start(SearchActivity.class);
                break;
        }
    }


    private void getBanner() {
        ObservableHelper.createObservable(RetrofitHelper.createService().getBanner())
                .subscribe(new SelectorObserver<BaseItem<List<BannerItem>>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {

                    }

                    @Override
                    protected void onSuccess(final BaseItem<List<BannerItem>> item) {
                        mRollPagerHeader.setAdapter(new LoopPagerAdapter(mRollPagerHeader) {
                            @Override
                            public View getView(ViewGroup container, int position) {
                                ImageView imageView = new ImageView(mContext);
                                Glide.with(mContext).load(mPre+item.getObjValue().get(position).getPicurl()).into(imageView);
                                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                return imageView;
                            }

                            @Override
                            public int getRealCount() {
                                return item.getObjValue().size();
                            }
                        });
                    }

                    @Override
                    protected void onFinish() {

                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }
                });
    }

    private void showList(String page) {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", page);
        map.put("numPerPage", Const.NUM_PER_PAGE);
        map.put("jobType", "1");
        ObservableHelper.createObservable(RetrofitHelper.createService().showZhaogongList(map))
                .subscribe(new SelectorObserver<BaseItem<ZhaoGongItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onSuccess(BaseItem<ZhaoGongItem> item) {
                        stopRefreshLoad();
                        List<ZhaoGongItem.ListBean> tmp = item.getObjValue().getList();
                        if (mIsRefresh) {
                            mPage = 1;
                            mZhaoGongItemList = tmp;
                            mAdapter.setNewData(mZhaoGongItemList);

                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mRefreshLayout.setLoadmoreFinished(false);
                            }

                        } else {
                            if (item.getObjValue().isIsLastPage()) {
                                mRefreshLayout.setLoadmoreFinished(true);
                            } else {
                                mPage++;
                                mRefreshLayout.setLoadmoreFinished(false);
                            }
                            mAdapter.addData(tmp);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        stopRefreshLoad();
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        if (mZhaoGongItemList == null) {
                            showLoadingDialog();
                        }
                    }
                });
    }
}
